<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-col :span="8">
              <a-form-model-item label="配方名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="formulaName">
                <a-select v-model="model.formulaName" placeholder="请选择配方">
                  <a-select-option v-for="(item,index) in brandList" :key="item.id">{{ item.fcName }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="配方编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fcNo">
                <a-select v-model="model.fcNo" placeholder="请选择配方编号">
                  <a-select-option v-for="(item,index) in brandList" :key="item.id">{{ item.fcNo }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="配料量(KG)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ingredientsAmount">
                <a-input-number v-model="model.ingredientsAmount" placeholder="请输入配料量" style="width: 100%" />
              </a-form-model-item>
            </a-col>
          </a-col>
        </a-row>
        <div class='add-box'>
          <a-button type="link" icon="plus" @click='addCavity'>添加液洗配方</a-button>
        </div>
        <a-form-model-item :labelCol="labelCol3" :wrapperCol="wrapperCol3">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol3">
                <div class='mark-infos'>
                  <a-table
                    ref="markTable"
                    size="middle"
                    :scroll="{x:false,y:false}"
                    bordered
                    :rowKey="(record = () => record.id)"
                    :columns="columns"
                    :dataSource="model.eneFormulaTslLiquidDetails"
                    :pagination="false"
                    :loading="loading"
                    class="j-table-force-nowrap1">
                    <template slot="action" slot-scope="text, record,index">
                      <a-popconfirm title="确定删除吗?" @confirm="() => deleteCavity(index)">
                        <a>删除</a>
                      </a-popconfirm>
                    </template>
                    <template v-for="col in ['ingredientsId','ratio','settingWeight']" :slot="col" slot-scope="text, record, indx">
                      <div :key="record.id">
                        <a-form-model-item v-if="col==='ingredientsId'">
                          <a-input v-model="record.ingredientsId"/>
                        </a-form-model-item>
                        <a-form-model-item v-else-if="col==='ratio'">
                          <a-input v-model="record.ratio" placeholder="请输入配比(%)" @input="onChange(record)"/>
                        </a-form-model-item >
                        <a-form-model-item v-else-if="col==='settingWeight'">
                          <a-input v-model="record.settingWeight" placeholder="请输入设定重量(KG)"/>
                        </a-form-model-item >
                      </div>
                    </template>
                  </a-table>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model-item>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction, postAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'EneFormulaTslLiquidForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        eneFormulaTslLiquidDetails: [
          {
            id: '',
            ingredientsId: '',
            ratio: '',
            settingWeight: ''
          }
        ]
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol3: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol3: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {},
      loading: false,
      brandList:[],// 品牌
      columns: [
        {
          title: '配料顺序',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'ingredientsId',
          scopedSlots: { customRender: 'ingredientsId' }
        },
        {
          title: '物料规格',
          align: 'center',
          dataIndex: 'ingredientsId',
          scopedSlots: { customRender: 'ingredientsId' }
        },
        {
          title: '配比（%）',
          align: 'center',
          dataIndex: 'ratio',
          scopedSlots: { customRender: 'ratio' }
        },
        {
          title: '设定重量（KG）',
          align: 'center',
          dataIndex: 'settingWeight',
          scopedSlots: { customRender: 'settingWeight' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        brandList:'/craft/eneFormulaTslBase/list',
        add: "/craft/eneFormulaTslLiquid/add",
        edit: "/craft/eneFormulaTslLiquid/edit",
        queryById: "/craft/eneFormulaTslLiquid/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  mounted() {
    this.getBrandList();
  },
  methods: {
    onChange(record){
      if(record){
        postAction('/craft/eneFormulaTslLiquid/compute/formula',{ingredientsAmount : this.model.ingredientsAmount, eneFormulaTslLiquidDetail : record}).then(
          (res)=>{
            if(res.success){
              console.log("到这了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊")
              this.$set(record, 'settingWeight', res.result);
            }
          })
      }
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    getBrandList(){
      getAction(this.url.brandList,{ pageSize: 100 }).then((res) => {
        if(res.success){
          this.brandList = res.result.records;
        }else{
          this.$message.warning(res.message)
        }
      })
    },
    addCavity() {
      if (!this.model.eneFormulaTslLiquidDetails) {
        this.$set(this.model, 'eneFormulaTslLiquidDetails', [])
      }
      this.model.eneFormulaTslLiquidDetails.push({
        id: '',
        ingredientsId: '',
        ingredientsId: '',
        ratio: '',
        settingWeight: ''
      })
    },
    deleteCavity(originalIndex) {
      let len = this.model.eneFormulaTslLiquidDetails.length
      if (len > 1) {
        this.model.eneFormulaTslLiquidDetails.splice(originalIndex, 1)
      } else {
        this.$message.error('最少保留一个液洗配方！')
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>
<style lang="scss" scoped>
.title {
  font-size: 24px;
  font-weight: bold;
}

.unit {
  padding-left: 4px;
  line-height: 40px;
}

.query-group-cust {
  width: 100% !important;
}

.ant-input-number {
  width: 100% !important;
}

.cavity-box {
  .title-box {
    height: 40px;
    padding: 0 5px;
    line-height: 40px;

    span {
      font-size: 24px;
      font-weight: bold;
      float: left;
    }

    .ant-btn-link {
      float: right;
    }
  }

  .mark-infos {
    position: relative;

    .editable-add-btn {
      position: absolute;
      top: 10px;
      right: 18px;
      z-index: 202;
    }
  }
}
::v-deep .ant-table-row {
  height: 48px !important;
}
</style>